<?php
/**
 * Created by PhpStorm.
 * User: tangchao
 * Date: 19-3-12
 * Time: 下午4:38
 */

namespace App\HttpController\Api;

use App\Lib\AliyunSdk\AliVod;
use App\Lib\Cache\VideoList;
use App\Model\Video;
use EasySwoole\Core\Component\Di;

class Index extends Base
{
    public function test()
    {
        $obj = new AliVod();
        $authRes = $obj->createUploadVideo('test2','test.mp4');
        $uploadAuth = json_decode(base64_decode($authRes->UploadAuth),true);
        $uploadAddress = json_decode(base64_decode($authRes->UploadAddress),true);
        $obj->initOssClient($uploadAuth,$uploadAddress);
        $res = $obj->uploadLocalFile($uploadAddress,'/home/tangchao/Videos/test.mp4');
        print_r($res);

    }

    public function getVideo()
    {
        $obj = new AliVod();
//        $res = $obj->getPlayInfo("f339008022944af18bb44217f924e3eb");
        $res = $obj->getPlayAuth("f7aa8c4467da4a869c5926c88c913dbb");
        return $this->response()->write(json_encode($res));
    }

    public function video()
    {
        $db = Di::getInstance()->get('MYSQL');
        return $this->writeJson(200,$db->get('video'));
    }

    public function getRedis()
    {
        $res = Di::getInstance()->get('REDIS')->get('tesla');
        return $this->writeJson(200,$res);
    }

    public function yaconf()
    {
        $res = \Yaconf::get('easyswoole.redis.host');
        return $this->writeJson(200,$res);
    }

    /**
     * 视频列表分页
     * @return bool
     * @throws \Exception
     */
    public function videoList()
    {
        //1.查询条件下有多少数据 count
        //2.获取到第几页的数据
        //a.mysql
//        $videoModel = new Video();
//        $res = $videoModel->getList($this->params['page'],[],0,['id'=>'ASC']);
        //b.从缓存数据拿数据
        $videoListCache = new VideoList();
        $res = $videoListCache->getData();
        return $this->writeJson(200,$res);
    }

    public function getStsInfo()
    {
        $obj = new AliVod();
//        $res = $obj->getPlayInfo("f339008022944af18bb44217f924e3eb");
        $res = $obj->getStsToken();
        return $this->response()->write(json_encode($res));
    }

    public function getPlayAuth()
    {
        $obj = new AliVod();
        $res = $obj->getPlayInfo("f339008022944af18bb44217f924e3eb");
        return $this->response()->write(json_encode($res));
    }

    public function getUploadAuth()
    {
        $data = $this->params;
        $obj = new AliVod();
        $authRes = $obj->createUploadVideo($data['title'],$data['filename']);
        return $this->response()->write(json_encode($authRes));
    }

    public function refreshUploadAuth()
    {
        $data = $this->params;
        $obj = new AliVod();
        $authRes = $obj->refreshUploadVideo($data['vid']);
        return $this->response()->write(json_encode($authRes));
    }

    public function getVideoInfo()
    {
        $obj = new AliVod();
        $authRes = $obj->getPlayInfo('a3f515dc71e34d2a85a8c33cbd8573be');
        print_r($authRes);
        return $this->response()->write(json_encode($authRes));
    }


}